package com.oak.interviewdemo.config.shiro;

import com.oak.interviewdemo.config.shiro.jwt.JwtFilter;
import com.oak.interviewdemo.config.shiro.jwt.JwtRealm;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SubjectFactory;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * springBoot整合jwt实现认证有三个不一样的地方，对应下面abc
 *
 * @Author: guoxy
 * @Date: 2020/5/14 23:15
 * @Description:
 * @Version: 1.0
 */
@Configuration
public class ShiroConfig {

    /**
     * 告诉shiro不要使用默认的DefaultSubject创建对象，因为不能创建Session
     */
    @Bean
    public SubjectFactory subjectFactory() {
        return new JwtDefaultSubjectFactory();
    }

    @Bean
    public Realm realm() {
        return new JwtRealm();
    }


    @Bean
    public DefaultWebSecurityManager securityManager() {
        /**
         * 1、构建security环境
         */
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 自定义realm
        securityManager.setRealm(realm());

        // 关闭shiro自带的session，详情见文档
        // http://shiro.apache.org/session-management.html#SessionManagement-
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        securityManager.setSubjectDAO(subjectDAO);
        // 禁止Subject的getSession方法
        securityManager.setSubjectFactory(subjectFactory());
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager());

        shiroFilter.setLoginUrl("/login");
        shiroFilter.setUnauthorizedUrl("/403");

        // 自定义过滤器jwt
        Map<String, Filter> filterMap = new HashMap<>();
        //这个地方其实另外两个filter可以不设置，默认就是
        filterMap.put("jwt", new JwtFilter());
        shiroFilter.setFilters(filterMap);

        Map<String, String> filterRuleMap = new HashMap<>();
        // 所有请求通过我们自己的JWT Filter
        filterRuleMap.put("/**", "jwt");
        // 放行Swagger接口
        filterRuleMap.put("/v2/api-docs", "anon");
        filterRuleMap.put("/swagger-resources/configuration/ui", "anon");
        filterRuleMap.put("/swagger-resources", "anon");
        filterRuleMap.put("/swagger-resources/configuration/security", "anon");
        filterRuleMap.put("/swagger-ui.html", "anon");
        filterRuleMap.put("/webjars/**", "anon");
        // 放行druid接口
        filterRuleMap.put("/druid/index.html", "anon");
        filterRuleMap.put("/druid/**", "anon");
        // 放行登录接口和其他不需要权限的接口
        filterRuleMap.put("/login", "anon");
        filterRuleMap.put("/checkLogin ", "anon");
        filterRuleMap.put("/css/**", "anon");
        filterRuleMap.put("/js/**", "anon");
        filterRuleMap.put("/**", "anon");

        shiroFilter.setFilterChainDefinitionMap(filterRuleMap);
        return shiroFilter;
    }


}

